package com.ruoyi.mes.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.LnyProInventoryMapper;
import com.ruoyi.mes.mapper.LnyRmOrderDetailMapper;
import com.ruoyi.mes.mapper.LnyStoreroomMapper;
import com.ruoyi.mes.utils.CodeUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.LnyStoragePutMapper;
import com.ruoyi.mes.service.ILnyStoragePutService;

/**
 * 入库管理Service业务层处理
 * 
 * @author CFL
 * @date 2024-04-28
 */
@Service
public class LnyStoragePutServiceImpl implements ILnyStoragePutService 
{
    @Autowired
    private LnyStoragePutMapper lnyStoragePutMapper;

    @Autowired
    private LnyStoreroomMapper lnyStoreroomMapper;

    @Autowired
    private LnyProInventoryMapper lnyProInventoryMapper;

    @Autowired
    private LnyRmOrderDetailMapper lnyRmOrderDetailMapper;

    /**
     * 查询入库管理
     * 
     * @param id 入库管理主键
     * @return 入库管理
     */
    @Override
    public LnyStoragePut selectLnyStoragePutById(Long id)
    {
        return lnyStoragePutMapper.selectLnyStoragePutById(id);
    }

    /**
     * 查询入库管理列表
     * 
     * @param lnyStoragePut 入库管理
     * @return 入库管理
     */
    @Override
    public List<LnyStoragePut> selectLnyStoragePutList(LnyStoragePut lnyStoragePut)
    {
        return lnyStoragePutMapper.selectLnyStoragePutList(lnyStoragePut);
    }

    /**
     * 新增入库管理
     * 
     * @param lnyStoragePut 入库管理
     * @return 结果
     */
    @Override
    public int insertLnyStoragePut(LnyStoragePut lnyStoragePut)
    {
        BaseController baseController = new BaseController();
        String username = baseController.getUsername();
        String put = CodeUtils.storagePutCodeCreat();
        Date nowDate = DateUtils.getNowDate();


        lnyStoragePut.setBpsSn(put);
        lnyStoragePut.setCreatedBy(username);
        lnyStoragePut.setCreatedTime(nowDate);
        return lnyStoragePutMapper.insertLnyStoragePut(lnyStoragePut);
    }

    /**
     * 入库修改库存数量
     *
     * @param lnyStoragePut 入库管理
     * @return 结果
     */
    public int updateStoreroomFromProduce(LnyStoragePut lnyStoragePut)
    {
        //产品入库(完工入库)    tips：退货入库暂不考虑，如果考虑应该也是这里
        if(lnyStoragePut.getProStrmId()!= null)
        {
            //修改库房管理表
            LnyStoreroom lnyStoreroom = new LnyStoreroom();

            lnyStoreroom.setId(lnyStoragePut.getStrmId());
            lnyStoreroom.setNum(lnyStoragePut.getNum());
            lnyStoreroomMapper.addLnyStoreroomById(lnyStoreroom);

            //修改库存总览表
            LnyProInventory lnyProInventory = new LnyProInventory();

            //根据产品id和库房id决定是做UPDATE还是INSERT
            lnyProInventory.setProId(lnyStoragePut.getProStrmId());
            lnyProInventory.setStrmId(lnyStoragePut.getStrmId());
            if(lnyProInventoryMapper.selectLnyProInventoryByProId(lnyProInventory)!=null)
            {
                lnyProInventory.setNum(lnyStoragePut.getNum());
                lnyProInventoryMapper.addLnyProInventoryByProId(lnyProInventory);
            }
            else
            {
                BaseController baseController = new BaseController();
                String username = baseController.getUsername();
                Date nowDate = DateUtils.getNowDate();

                lnyProInventory.setInvState(0);     //Long.valueOf(0)
                lnyProInventory.setProNum(lnyStoragePut.getNum());
                //此表中产品和物料共用ProNum表示数量

                lnyProInventory.setCreatedBy(username);
                lnyProInventory.setCreatedTime(nowDate);

                lnyProInventoryMapper.insertLnyProInventory(lnyProInventory);
            }

            //修改入库表状态和已入库数量     和入库日期
            lnyStoragePut.setBpsDate(DateUtils.getNowDate());
            lnyStoragePutMapper.updateLnyStoragePut(lnyStoragePut);
            return 1;
        }
        else
        {
            //出错了才回来这
            return 0;
        }
    }

    /**
     * 采购入库修改库存数量
     *
     * @param lnyStoragePut 入库管理
     * @return 结果
     */
    public int updateStoreroomFromBuy(List<LnyStoragePut> lnyStoragePut)
    {
        //物料入库
        for (int i = 0; i < lnyStoragePut.size(); i++){
            LnyStoragePut lnyStoragePutTemp = lnyStoragePut.get(i);
            if(lnyStoragePutTemp.getMatId()!=null) {
                //修改库房管理表
                LnyStoreroom lnyStoreroom = new LnyStoreroom();

                lnyStoreroom.setId(lnyStoragePutTemp.getStrmId());
                lnyStoreroom.setNum(lnyStoragePutTemp.getNum());
                lnyStoreroomMapper.addLnyStoreroomById(lnyStoreroom);

                //修改采购订单明细表
                LnyRmOrderDetail lnyRmOrderDetail = new LnyRmOrderDetail();
                lnyRmOrderDetail.setMatId(lnyStoragePutTemp.getMatId());
                lnyRmOrderDetail.setPurchaseSn(lnyStoragePutTemp.getPurchaseSn());
                lnyRmOrderDetail.setNum(lnyStoragePutTemp.getNum());
//              lnyRmOrderDetail.setBpsRenum(lnyRmOrderDetail.getBpsRenum()+lnyStoragePutTemp.getNum());
                lnyRmOrderDetailMapper.updateLnyRmOrderDetailFromBuy(lnyRmOrderDetail);

                //修改库存总览表
                LnyProInventory lnyProInventory = new LnyProInventory();

                //根据物料id和库房id决定是做UPDATE还是INSERT
                lnyProInventory.setMatId(lnyStoragePutTemp.getMatId());
                lnyProInventory.setStrmId(lnyStoragePutTemp.getStrmId());
                if (lnyProInventoryMapper.selectLnyProInventoryByMatId(lnyProInventory) != null)
                {
                    lnyProInventory.setNum(lnyStoragePutTemp.getNum());
                    lnyProInventoryMapper.addLnyProInventoryByMatId(lnyProInventory);
                }
                else
                {
                    BaseController baseController = new BaseController();
                    String username = baseController.getUsername();
                    Date nowDate = DateUtils.getNowDate();

                    lnyProInventory.setInvState(1);     //Long.valueOf(1)
                    lnyProInventory.setProNum(lnyStoragePutTemp.getNum());
                    //此表中产品和物料共用ProNum表示数量

                    lnyProInventory.setCreatedBy(username);
                    lnyProInventory.setCreatedTime(nowDate);

                    lnyProInventoryMapper.insertLnyProInventory(lnyProInventory);
                }
            }
        }
        return 1;
    }

    /**
     * 采购入库查询物料相关的接口
     *
     * @param purchaseSn 采购订单编号
     * @return 结果
     */
    public List<LnyRmOrderDetail> selectFromBuy(String purchaseSn)
    {
        return lnyRmOrderDetailMapper.selectLnyRmOrderDetailListByPurchaseSn(purchaseSn);
    }

    /**
     * 修改入库管理
     * 
     * @param lnyStoragePut 入库管理
     * @return 结果
     */
    @Override
    public int updateLnyStoragePut(LnyStoragePut lnyStoragePut)
    {
        return lnyStoragePutMapper.updateLnyStoragePut(lnyStoragePut);
    }

    /**
     * 批量删除入库管理
     * 
     * @param ids 需要删除的入库管理主键
     * @return 结果
     */
    @Override
    public int deleteLnyStoragePutByIds(Long[] ids)
    {
        return lnyStoragePutMapper.deleteLnyStoragePutByIds(ids);
    }

    /**
     * 删除入库管理信息
     * 
     * @param id 入库管理主键
     * @return 结果
     */
    @Override
    public int deleteLnyStoragePutById(Long id)
    {
        return lnyStoragePutMapper.deleteLnyStoragePutById(id);
    }
}
